#include <iostream>
#include <pthread.h>
#include <string>
#include <cstdio>
#include <unistd.h>
#include "cond.hpp"
using namespace cond_module;
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
cond c(mutex);
//pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
int turn; 
void* print(void* arg)
{
    char id=*(char*)arg;
    int myturn = id=='A'?0:1;
    while(true)
    {
        pthread_mutex_lock(&mutex);
        while(turn != myturn)
        {
            //pthread_cond_wait(&cond,&mutex);
            c.wait();
        }
        printf("线程%c打印-我是线程%c\n",*(char*)arg,*(char*)arg);

        turn=1-turn;
        //pthread_cond_signal(&cond);
        c.wake();    
        pthread_mutex_unlock(&mutex);
        sleep(1);
    }
    return nullptr;
}
int main()
{
    
    pthread_t p1;
    pthread_t p2;
    char a='A';
    char b='B';
    pthread_create(&p1,nullptr,print,&a);
    pthread_create(&p2,nullptr,print,&b);
    pthread_join(p1,nullptr);
    pthread_join(p2,nullptr);
    while(true)
    {
        //pthread_cond_signal(&cond);
        //c.wake();
        sleep(1);
    }
    return 0;
}